<?php


class Sessions {
	var $context;
	
	function Sessions(&$context){
		$this->context = &$context;
		$this->_init();
	}
	
	function _init(){
		if(!session_id()){
			session_start();
		}
		if(!session_id()){
			//$this->caller->errorHandler->addWarning('Sessions class cannot initialize',__FILE__,__LINE__);
		}else{
			$this->_initSessionId();
		}
	}
	
	function _initSessionId(){
		if(!$this->context->cookies->isExist('UGMSESSID')){
			$this->context->cookies->create('UGMSESSID',session_id());			
		}else{
			session_id($this->Context->cookies->get('UGMSESSID'));
		}
	}
	
	function getSessionId(){
		$this->_init();
		return session_id();
	}
	
	function isExist($field=''){
			$this->_init();
			if(session_is_registered($field)){
				return true;
			}
			return false;
	}
	
	function get($field=''){
			$this->_init();
		if($this->isExist($field)){
			return $_SESSION[$field];
		}
		return false;
	}
	
	function set($field='',$value=''){
		$this->_init();
		if(!$this->isExist($field)){
			$this->create($field);
		}
		$_SESSION[$field] = $value;
	}
	
	function create($field=''){
		$this->_init();
		if($this->isExist($field)){
			//$this->caller->errorHandler->addWarning('Cannot create sessions called '.$field.' again.',__FILE__,__LINE);
			return false;
		}
		session_register($field);
		return true;
	}
	
	function clear($field=''){
		$this->set($field,NULL);
	}
	
	function remove($field=''){
		$this->_init();
		if(!$this->isExist($field)){
			//$this->caller->errorHandler->addWarning('Cannot remove sessions called '.$field.' . Session not found',__FILE__,__LINE);
			return false;
		}
		$value = $this->get($field);
		session_unregister($field);
		return $value;
	}

}
?>